/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.model;

import java.io.Serializable;
import java.util.Set;
import java.util.TreeSet;

/**
 * This class represents a flight.
 * <p>
 * 
 * You should use it directly only to confirm and count free seats.
 * You should use {@link Log#executeOp(Op, Company)} 
 * (with previous call {@link Log#annotate(FlightId, CustomerId, ProxyId)}
 * and {@link Log#prepared(OpId, Quorum)} ).
 * to seat a customer in the plane safely.
 * <p>
 * 
 * Warning: this class is not thread safe, but if you use it from 
 * one single server instance this is not a problem.
 * 
 * @author drodenas
 */
public final class Flight implements Serializable {
	private static final long serialVersionUID = -1959632246576092926L;
	protected Flight() { logCtx=null;id=null; }
	
	private static final Logger log = Logger.get("ModelFlight");
    private static final int MAX_CAPACITY = 120;
    
    private final String logCtx;
    private final FlightId id;
    private final Set<CustomerId> customers = new TreeSet<>();

    Flight(ServerId serverId, FlightId id) {
        logCtx = serverId+"|"+id;
        this.id = id;
    }
    
    /**
     * Confirms that a customer is seated in a flight.
     * 
     * @param customer
     * @return <code>true</code> if is inside, even if there is overbooking
     */
    public boolean confirm(CustomerId customer) {
        if (customer == null) throw new NullPointerException("Illegal argument: customer cannot be null");
        
        return customers.contains(customer);
    }
    
    /**
     * Seats a customer in the plane.
     * <p>
     * 
     * This method always seat a customer in a flight.
     * This method may produce overbooking.
     * This method should not be used directly (except T0),
     * you should use {@link Log#executeOp(Op, Company)} instead.
     * 
     * @param customer the customer that you want to seat
     * @see Op#execute(Company)
     * @see Log#executeOp(Op, Company)
     * @see Log#annotate(FlightId, CustomerId, ProxyId)
     * @see Log#prepared(OpId, Quorum)
     */
    public void seat(CustomerId customer) {
        if (customer == null) throw new NullPointerException("Illegal argument: customer cannot be null");
        log.debug(logCtx, "seat("+customer+") was here:"+customers.contains(customer));
        
        customers.add(customer);
    }
    
    /**
     * Gets the number of free seats.
     * 
     * @return 0 if no free seats, 
     *         positive number if more seats can be done,
     *         negative number if there is overbooking
     */
    public int query() {
        return MAX_CAPACITY - customers.size();
    }

    @Override
    public String toString() {
        return id+"="+customers;
    }
    
    
    
}
